]> git.r.bdr.sh - rbdr/map/blame - Map/Presentation/Base Components/MapRender/MapEdges.swift
Update license URL
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapEdges.swift
CommitLineData
5e8ff485
RBR
1import SwiftUI
2
3struct MapEdges: View {
4
5e8ff485
RBR
5 let mapSize: CGSize
6 let lineWidth: CGFloat
7 let vertexSize: CGSize
8 let edges: [MapEdge]
9
10 let arrowheadSize = CGFloat(10.0)
11
5e8ff485
RBR
12 var body: some View {
13 ForEach(edges, id: \.id) { edge in
14 Path { path in
15
16 // First we transform edges from percentage to map coordinates
17 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
18 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
19
20 let slope = (destination.y - origin.y) / (destination.x - origin.x)
21 let angle = atan(slope)
22 let multiplier = CGFloat(slope < 0 ? -1.0 : 1.0)
23 let upperAngle = angle - CGFloat.pi / 4.0
24 let lowerAngle = angle + CGFloat.pi / 4.0
25
26 let offsetOrigin = CGPoint(
27 x: origin.x + multiplier * (vertexSize.width / 2.0) * cos(angle),
28 y: origin.y + multiplier * (vertexSize.height / 2.0) * sin(angle))
29 let offsetDestination = CGPoint(
30 x: destination.x - multiplier * (vertexSize.width / 2.0) * cos(angle),
31 y: destination.y - multiplier * (vertexSize.height / 2.0) * sin(angle))
32
33 path.move(to: offsetOrigin)
34 path.addLine(to: offsetDestination)
35
36 if edge.arrowhead {
37 path.move(to: offsetDestination)
38 path.addLine(
39 to: CGPoint(
40 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
41 y:
42 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
43
44 path.move(to: offsetDestination)
45 path.addLine(
46 to: CGPoint(
47 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
48 y:
49 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
50 }
51 path.move(to: offsetDestination)
52 path.closeSubpath()
53 }.applying(
54 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
fdb4633d 55 ).stroke(Color.map.vertexColor, lineWidth: lineWidth)
5e8ff485
RBR
56 }
57 }
58
59 func h(_ dimension: CGFloat) -> CGFloat {
60 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
61 }
62
63 func w(_ dimension: CGFloat) -> CGFloat {
64 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
65 }
66}
67
e2c37ac1
RBR
68#Preview {
69 MapEdges(
70 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
71 vertexSize: CGSize(width: 25.0, height: 25.0),
72 edges: [
73 MapEdge(
74 id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2),
75 arrowhead: true)
76 ])
5e8ff485 77}